package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.ExpressActivity;
import flc.ast.adapter.ExpressBannerAdapter;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.RecAdapter;
import flc.ast.databinding.FragmentExpressBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ExpressFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ExpressFragment extends BaseNoModelFragment<FragmentExpressBinding> {

    private ExpressBannerAdapter expressBannerAdapter;
    private RecAdapter recAdapter;
    private HotAdapter hotAdapter;


    @Override
    protected int onCreate() {
        return R.layout.fragment_express;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.tvMoreExpress.setOnClickListener(this);
        mDataBinding.expressBanner.isAutoLoop(true);
        //动漫推荐
        mDataBinding.rvRecList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        recAdapter=new RecAdapter();
        mDataBinding.rvRecList.setAdapter(recAdapter);
        recAdapter.setOnItemClickListener(this);
        //热门动漫
        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext,2));
        hotAdapter=new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvMoreExpress:
                startActivity(ExpressActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getExpressBannerData();
        getRecData();
        getHotData();
    }

    private void getExpressBannerData(){
        StkResApi.getTagResourceList(null, AppConfig.BANNER_DATA, StkResApi.createParamMap(0, 10),false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        expressBannerAdapter=new ExpressBannerAdapter(result);
                        mDataBinding.expressBanner.setAdapter(expressBannerAdapter, true);//false不无限循环
                        mDataBinding.expressBanner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object data, int position) {
                                Log.e("TAG", "OnBannerClick: " );
                                StkResBean bean = (StkResBean) expressBannerAdapter.getData(position);
                                BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
                            }
                        });
                    }
                }
            }
        });
    }

    private void getRecData(){
        StkResApi.getTagResourceList(null, AppConfig.REC_DATA, StkResApi.createParamMap(0, 10),false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getHotData(){
        StkResApi.getTagResourceList(null, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 4),false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotAdapter.setList(result);
                    }
                }
            }
        });
    }

}